using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using SouthernLand.Common.BOL.NhaDat;

namespace SouthernLand.Common.DAL.NhaDat
{
    public class RaovatSaveDAL : BaseDAL
    {
        #region ObjIDataReader

        /// <summary>
        /// Returns a new RaovatSave instance filled with the DataReader's current record data 
        /// </summary>
        private RaovatSave GetRaovatSaveFromReader(IDataReader dr)
        {
            var obj = new RaovatSave();
            obj.RaovatSaveID = (dr["RaovatSaveID"] is DBNull) ? Int32.MinValue : (Int32) dr["RaovatSaveID"];
            obj.RaovatID = (dr["RaovatID"] is DBNull) ? Int32.MinValue : (Int32) dr["RaovatID"];
            obj.UserID = (dr["UserID"] is DBNull) ? String.Empty : (String) dr["UserID"];
            obj.AddedDate = (dr["AddedDate"] is DBNull) ? DateTime.MinValue : (DateTime) dr["AddedDate"];
            return obj;
        }

        #endregion

        /// <summary>
        /// Returns a collection of RaovatSave with the data read from the input DataReader
        /// </summary>
        private List<RaovatSave> GetRaovatSaveCollectionFromReader(IDataReader dr)
        {
            var obj = new List<RaovatSave>();
            while (dr.Read())
                obj.Add(GetRaovatSaveFromReader(dr));
            return obj;
        }

        public int Insert(RaovatSave obj)
        {
            using (SqlConnection myConn = GetConnectionString())
            {
                var myCommand = new SqlCommand("w4u_RaovatSave_Insert", myConn);
                myCommand.CommandType = CommandType.StoredProcedure;
                myCommand.Parameters.Add("@RaovatSaveID", SqlDbType.Int).Direction = ParameterDirection.Output;
                myCommand.Parameters.Add("@RaovatID", SqlDbType.Int).Value = obj.RaovatID;
                myCommand.Parameters.Add("@UserID", SqlDbType.VarChar).Value = obj.UserID;
                myCommand.Parameters.Add("@AddedDate", SqlDbType.DateTime).Value = obj.AddedDate;

                int ret = myCommand.ExecuteNonQuery();
                return (int) myCommand.Parameters["@RaovatSaveID"].Value;
            }
        }

        public bool Update(RaovatSave obj)
        {
            using (SqlConnection myConn = GetConnectionString())
            {
                var myCommand = new SqlCommand("w4u_RaovatSave_Update", myConn);
                myCommand.CommandType = CommandType.StoredProcedure;
                myCommand.Parameters.Add("@RaovatSaveID", SqlDbType.Int).Value = obj.RaovatSaveID;
                myCommand.Parameters.Add("@RaovatID", SqlDbType.Int).Value = obj.RaovatID;
                myCommand.Parameters.Add("@UserID", SqlDbType.VarChar).Value = obj.UserID;
                myCommand.Parameters.Add("@AddedDate", SqlDbType.DateTime).Value = obj.AddedDate;

                int ret = myCommand.ExecuteNonQuery();
                return (ret == 1);
            }
        }

        public bool Delete(Int32 RaovatSaveID)
        {
            using (SqlConnection myConn = GetConnectionString())
            {
                var myCommand = new SqlCommand("w4u_RaovatSave_Delete", myConn);
                myCommand.CommandType = CommandType.StoredProcedure;
                myCommand.Parameters.Add("@RaovatSaveID", SqlDbType.Int).Value = RaovatSaveID;

                int ret = myCommand.ExecuteNonQuery();
                return (ret == 1);
            }
        }

        /// <summary>
        /// Use in case when user delete a raovat news then that news in 
        /// raovatSave must be deleted
        /// </summary>
        /// <param name="RaovatID"></param>
        /// <returns></returns>
        public bool DeleteByRaovatId(Int32 RaovatID)
        {
            using (SqlConnection myConn = GetConnectionString())
            {
                var myCommand = new SqlCommand("w4u_RaovatSave_DeleteByRaovatID", myConn);
                myCommand.CommandType = CommandType.StoredProcedure;
                myCommand.Parameters.Add("@RaovatID", SqlDbType.Int).Value = RaovatID;

                int ret = myCommand.ExecuteNonQuery();
                return (ret == 1);
            }
        }

        /// <summary>
        /// Retrieves the w4u_RaovatSave with the specified ID
        /// </summary>
        public RaovatSave GetRaovatSaveByID(Int32 RaovatSaveID)
        {
            using (SqlConnection myConn = GetConnectionString())
            {
                var myCommand = new SqlCommand("w4u_RaovatSave_GetRaovatSaveByID", myConn);
                myCommand.CommandType = CommandType.StoredProcedure;
                myCommand.Parameters.Add("@RaovatSaveID", SqlDbType.Int).Value = RaovatSaveID;

                SqlDataReader dr = myCommand.ExecuteReader();
                return (dr.Read() ? GetRaovatSaveFromReader(dr) : null);
            }
        }

        //Get All RaovatSave
        public List<RaovatSave> GetAllRaovatSave(int PageIndex, int PageSize)
        {
            using (SqlConnection myConn = GetConnectionString())
            {
                var myCommand = new SqlCommand("w4u_RaovatSave_GetAllRaovatSave", myConn);
                myCommand.CommandType = CommandType.StoredProcedure;
                myCommand.Parameters.Add("@PageIndex", SqlDbType.Int).Value = PageIndex;
                myCommand.Parameters.Add("@PageSize", SqlDbType.Int).Value = PageSize;
                SqlDataReader dr = myCommand.ExecuteReader();
                return GetRaovatSaveCollectionFromReader(dr);
            }
        }

        // Get Count All records of Table  RaovatSave
        public int GetCountRaovatSave()
        {
            using (SqlConnection myConn = GetConnectionString())
            {
                var myCommand = new SqlCommand("w4u_RaovatSave_GetCountRaovatSave", myConn);
                myCommand.CommandType = CommandType.StoredProcedure;
                return (int) myCommand.ExecuteScalar();
            }
        }

        // Get RaovatSaveByUserID
        public List<RaovatSave> GetRaovatSaveByUserID(String UserID, int PageIndex, int PageSize)
        {
            using (SqlConnection myConn = GetConnectionString())
            {
                var myCommand = new SqlCommand("w4u_RaovatSave_GetRaovatSaveByUserID", myConn);
                myCommand.CommandType = CommandType.StoredProcedure;
                myCommand.Parameters.Add("@UserID", SqlDbType.VarChar).Value = UserID;
                myCommand.Parameters.Add("@PageIndex", SqlDbType.Int).Value = PageIndex;
                myCommand.Parameters.Add("@PageSize", SqlDbType.Int).Value = PageSize;
                SqlDataReader dr = myCommand.ExecuteReader();
                return GetRaovatSaveCollectionFromReader(dr);
            }
        }

        // Get RaovatSaveCountByUserID
        public int GetRaovatSaveCountByUserID(String UserID)
        {
            using (SqlConnection myConn = GetConnectionString())
            {
                var myCommand = new SqlCommand("w4u_RaovatSave_GetRaovatSaveCountByUserID", myConn);
                myCommand.CommandType = CommandType.StoredProcedure;
                myCommand.Parameters.Add("@UserID", SqlDbType.VarChar).Value = UserID;
                return (int) myCommand.ExecuteScalar();
            }
        }
    }
}